import { RouteConfig } from 'react-router-config';

import { lazy } from 'react';
import { AnswerDetail, AnswerList } from '../page/answer-list/index';
import AnswerSettingSuccess from '../page/answer-setting/feedback/success';
import AnswerSetting from '../page/answer-setting';
import ChangePassword from '../page/change-password';
import Success from
  '../page/questionnaire-setting/components/result-page/success';
import Login from '../page/index/login';
import {
  Container as QuestionnaireSetting,
  TemplateIncrease,
  TemplateUpdate,
  QuestionnaireDetail
} from '../page/questionnaire-setting';
import QuestionAnswerCloseResult from '../page/questionnaire/components/error';
import QuestionAnswerSuccess from '../page/questionnaire/components/success';
import
QuestionAnswerVerification
  from '../page/questionnaire/components/verification';
import
QuestionAnswerBasicInfo
  from '../page/questionnaire/components/basic-info';
import QuestionAnswerQuestion from '../page/questionnaire/components/question';
import ManagerQuestionnaire from '../page/manager-questionnaire';

export const QuestionAnswer = lazy(
  () => import('../page/questionnaire')
);

export const Home = lazy(
  () => import('../outter-page/home')
);

export const pathMatch = (path: string, pathPattern: string) => {
  const pathArr = path.split('/');
  const pathPatternArr = pathPattern.split('/');

  if (pathArr.length !== pathPatternArr.length) {
    return false;
  }

  for (let i = 0; i < pathArr.length; i += 1) {
    const pathPatternItem = pathPatternArr[i];
    const pathItem = pathArr[i];

    if (
      pathPatternItem[0] !== ':'
      && pathItem !== pathPatternItem
    ) {
      return false;
    }
  }

  return true;
};

export const questionAnswerQuestionPathObj = {
  path: '/questionAnswer/home/:questionnaireUuid/question/:index',
  component: QuestionAnswerQuestion,
  exact: true,
};

const config: RouteConfig[] = [
  {
    path: '/',
    component: Login,
    exact: true,
  },
  {
    path: '/questionAnswer/home/:questionnaireUuid',
    component: QuestionAnswer,
    routes: [
      {
        path: '/questionAnswer/home/:questionnaireUuid/closeResult',
        component: QuestionAnswerCloseResult,
        exact: true,
      },
      {
        path: '/questionAnswer/home/:questionnaireUuid/successResult',
        component: QuestionAnswerSuccess,
        exact: true,
      },
      {
        path: '/questionAnswer/home/:questionnaireUuid/basicInfo/:index',
        component: QuestionAnswerBasicInfo,
        exact: true,
      },
      {
        path: '/questionAnswer/home/:questionnaireUuid/verfication',
        component: QuestionAnswerVerification,
        exact: true,
      },
      questionAnswerQuestionPathObj
    ]
  },
  {
    path: '/home',
    component: Home,
    routes: [
      {
        path: '/home/managerQuestionnaire',
        component: ManagerQuestionnaire,
        exact: true,
      },
      {
        path: '/home/answerDetail',
        component: AnswerDetail,
        exact: true,
      },
      {
        // eslint-disable-next-line max-len
        path: '/home/answerSetting/:mergeUuid/:templateUuid/:templateTitle/:questionnaireUuid/:questionnaireName',
        component: AnswerSetting,
        exact: true,
      },
      {
        path: '/home/answerSetting',
        component: AnswerSetting,
        exact: true,
      },
      {
        path: '/home/answerSetting/success',
        component: AnswerSettingSuccess,
        exact: true,
      },
      {
        path: '/home/answerList',
        component: AnswerList,
        exact: true,
      },
      {
        path: '/home/changePassword',
        component: ChangePassword,
        exact: true,
      },
      {
        path: '/home/questionnaireSettings',
        component: QuestionnaireSetting,
        exact: true,
      },
      {
        path: '/home/templateIncrease',
        component: TemplateIncrease,
        exact: true,
      },
      {
        path: '/home/templateUpdate/:uuid',
        component: TemplateUpdate,
        exact: true,
      },
      {
        path: '/home/questionnaireDetail/:templateUuid/:questionnaireUuid',
        component: QuestionnaireDetail,
        exact: true,
      },
      {
        path: '/home/success',
        component: Success,
        exact: true,
      },
    ],
  }
];

export default config;
